//
//  MoreVC.m
//  夺宝吧
//
//  Created by xiaocai on 15/6/25.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "MoreVC.h"
#import "WYPublicWeb.h"
#import "UMengRgt.h"
#import "APService.h"
#import "ShoppingCarVC.h"

#define ATCL_URL @"api/ApiMisc/article?id="

#define FEEDBACK_URL @"api/ApiUser/feedback"

@interface MoreVC ()
{
    __weak IBOutlet UIButton *outBtn;
    __weak IBOutlet UIView *navi;
    
    __weak IBOutlet UIView *whiteBg;
    //显示lab
    __weak IBOutlet UILabel *banbenLab;
    __weak IBOutlet UILabel *aboutLab;
    __weak IBOutlet UILabel *helpLab;
    __weak IBOutlet UILabel *fuwuLab;
    __weak IBOutlet UILabel *yijianLab;
    __weak IBOutlet UILabel *tuisongLab;
    __weak IBOutlet UILabel *qingliLab;
    
    __weak IBOutlet UISwitch *tuisongBtn;
    
    __weak IBOutlet UILabel *numLab;
    
    __weak IBOutlet UILabel *ysLab;
    __weak IBOutlet UIButton *cjwtLab;
    
    __weak IBOutlet UILabel *sizeLab;
    
    __weak IBOutlet UISwitch *pushBtn;
    
    
    __weak IBOutlet UIButton *quitBtn;
}
@end

@implementation MoreVC
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //设置quitBtn在不同状态下的外观
    [quitBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [quitBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    
    //是否显示退出登录按钮
    if (! [UserInfo isUserLogin]){ //没有用户登录
        quitBtn.hidden = YES;
        quitBtn.enabled = NO;
        quitBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    }
    else{
        quitBtn.hidden = NO;
        quitBtn.enabled = YES;    //有用户登录
        quitBtn.layer.borderColor = [UIColor redColor].CGColor;
    }

    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.automaticallyAdjustsScrollViewInsets = YES;
    outBtn.layer.masksToBounds = YES;
    outBtn.layer.cornerRadius = 2;
    outBtn.layer.borderColor = [UIColor darkGrayColor].CGColor;
    outBtn.layer.borderWidth = 1.0f;
    navi.backgroundColor = QBB_RED_COLOR;
    //分割线的处理
    [banbenLab addSubview:[WYPublic getFenGexian:0 :(banbenLab.frame.size.height-0.5) :WYMWITH :0.5]];
    [aboutLab addSubview:[WYPublic getFenGexian:0 :(aboutLab.frame.size.height-0.5) :WYMWITH :0.5]];
    [helpLab addSubview:[WYPublic getFenGexian:0 :(helpLab.frame.size.height-0.5) :WYMWITH :0.5]];
    [fuwuLab addSubview:[WYPublic getFenGexian:0 :(fuwuLab.frame.size.height-0.5) :WYMWITH :0.5]];
    [yijianLab addSubview:[WYPublic getFenGexian:0 :(yijianLab.frame.size.height-0.5) :WYMWITH :0.5]];
    [tuisongLab addSubview:[WYPublic getFenGexian:0 :(tuisongLab.frame.size.height-0.5) :WYMWITH :0.5]];
    [ysLab addSubview:[WYPublic getFenGexian:0 :(ysLab.frame.size.height-0.5) :WYMWITH :0.5]];
    [cjwtLab addSubview:[WYPublic getFenGexian:0 :(cjwtLab.frame.size.height+6-0.5) :WYMWITH :0.5]];
    [qingliLab addSubview:[WYPublic getFenGexian:0 :(qingliLab.frame.size.height-0.5) :WYMWITH :0.5]];

    tuisongBtn.tintColor = QBB_RED_COLOR;
    tuisongBtn.onTintColor = QBB_RED_COLOR;
    
    //版本
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    if (app_Version != nil) {
        numLab.text = [NSString stringWithFormat:@"V%@",app_Version];
    }
    //推送开关
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"PUSH_FLAG"]) {
        [pushBtn setOn:NO];
    }else{
       [pushBtn setOn:YES];
    }
    //缓存
    float f_size = (float)[[SDImageCache sharedImageCache] getSize]*1.0/1024/1024;
    sizeLab.text = [NSString stringWithFormat:@"%.2fM",f_size];
    
   }


- (IBAction)outBtnOnClick:(id)sender {
    //退出登录
    
//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"退出" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//        
//    }];
//    UIAlertAction *quitAction = [UIAlertAction actionWithTitle:@"退出登录" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//        
//    }];
//    
//    [alertController addAction:cancelAction];
    
//    UIActionSheet *actionSheet = [UIActionSheet alloc]initWithTitle:@"退出" delegate:self cancelButtonTitle:<#(nullable NSString *)#> destructiveButtonTitle:<#(nullable NSString *)#> otherButtonTitles:<#(nullable NSString *), ...#>, nil
    
    [UserInfo deleteUserInfo];
    [[WYDataBase sharedDataBase]deleteDatabase];
    //退出登录要刷新购物车
    [ShoppingCarVC shareShoppingCarVC].firstFlag = YES;
    [self.navigationController popViewControllerAnimated:YES];
    [[WYMainTabBarVC shareMainTaBarVC]changeSelectedWithIndex:0];
}
//返回按钮的监听事件
- (IBAction)backBtnOnClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)btnsOnClick:(UIButton *)sender {
    switch (sender.tag) {
        case 1:/***********关于我们**************/
        {
            WYPublicWeb *web = [[WYPublicWeb alloc]init];
            web.mtitle = @"关于我们";
            NSString *newIp = [DBB_IP stringByReplacingOccurrencesOfString:@"?/" withString:@""];
            web.urlStr = [NSString stringWithFormat:@"%@%@%@",newIp,ATCL_URL,@"dbba_aboutus"];
            [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:web animated:YES];
        }
            break;
        case 2:/***********帮助中心**************/
        {
            WYPublicWeb *web = [[WYPublicWeb alloc]init];
            web.mtitle = @"帮助中心";
            NSString *newIp = [DBB_IP stringByReplacingOccurrencesOfString:@"?/" withString:@""];
            web.urlStr = [NSString stringWithFormat:@"%@%@%@",newIp,ATCL_URL,@"dbba_help"];
            [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:web animated:YES];
        }
            break;
        case 3:/***********服务协议**************/
        {
            WYPublicWeb *web = [[WYPublicWeb alloc]init];
            web.mtitle = @"服务协议";
            NSString *newIp = [DBB_IP stringByReplacingOccurrencesOfString:@"?/" withString:@""];
            web.urlStr = [NSString stringWithFormat:@"%@%@%@",newIp,ATCL_URL,@"dbba_service"];
            NSLog(@"url=%@",web.urlStr);
            [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:web animated:YES];
        }
            break;
        case 4:/***********隐私协议**************/
        {
            WYPublicWeb *web = [[WYPublicWeb alloc]init];
            web.mtitle = @"隐私协议";
            NSString *newIp = [DBB_IP stringByReplacingOccurrencesOfString:@"?/" withString:@""];
            web.urlStr = [NSString stringWithFormat:@"%@%@%@",newIp,ATCL_URL,@"dbba_privacy"];
            [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:web animated:YES];
        }
            break;
        case 5:/***********常见问题**************/
        {
            WYPublicWeb *web = [[WYPublicWeb alloc]init];
            web.mtitle = @"常见问题";
            NSString *newIp = [DBB_IP stringByReplacingOccurrencesOfString:@"?/" withString:@""];
            web.urlStr = [NSString stringWithFormat:@"%@%@%@",newIp,ATCL_URL,@"dbba_faq"];
            [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:web animated:YES];
        }
            break;
        case 6:/***********意见反馈**************/
        {
            WYPublicWeb *web = [[WYPublicWeb alloc]init];
            web.mtitle = @"用户反馈";
            NSString *newIp = [DBB_IP stringByReplacingOccurrencesOfString:@"?/" withString:@""];
            web.urlStr = [NSString stringWithFormat:@"%@%@",newIp,FEEDBACK_URL];
            [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:web animated:YES];

//            self.navigationController.navigationBar.hidden = NO;
//            [self.navigationController pushViewController:[UMFeedback feedbackViewController]
//                                                 animated:YES];
        }
            break;
        case 7:/***********清理缓存**************/
        {
            [self clearTmpPics];
            
//            [self deleteDataBaseFiles];
            [[WYDataBase sharedDataBase]deleteDatabase];
        }
            break;
        default:
            break;
    }
}
//删除数据库文件
- (void)deleteDataBaseFiles{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:NULL];
    NSEnumerator *e = [contents objectEnumerator];
    NSString *filename;
    while ((filename = [e nextObject])) {
        [fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:NULL];
    }

}
//清理缓存
- (void)clearTmpPics
{
    [WYPublic addZheZhaoToWindowWithTitle:nil];
    [[SDImageCache sharedImageCache] clearDisk];
    
    double delayInSeconds = 1.5;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [WYPublic removeZheZhaoToWindow];
        //缓存
        float f_size = (float)[[SDImageCache sharedImageCache] getSize]*1.0/1024/1024;
        sizeLab.text = [NSString stringWithFormat:@"%.2fM",f_size];
    });

}
//推送开关
- (IBAction)tsKaiguan:(UISwitch *)sender {
    //设置推送标志
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if (sender.isOn == NO) {
        [user setBool:YES forKey:@"PUSH_FLAG"];
        //update by weienjie 20150917
        // JPush
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
            //可以添加自定义categories
            [APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                           UIUserNotificationTypeSound |
                                                           UIUserNotificationTypeAlert)
                                               categories:nil];
        } else {
            //categories 必须为nil
            [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                           UIRemoteNotificationTypeSound |
                                                           UIRemoteNotificationTypeAlert)
                                               categories:nil];
        }
        //update end
    }else{
        [user setBool:NO forKey:@"PUSH_FLAG"];
        [[UIApplication sharedApplication] unregisterForRemoteNotifications];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
